package com.mju.byc.controllers;

import com.mju.byc.Repository.RoleRepository;
import com.mju.byc.Repository.UserRepository;
import com.mju.byc.pojo.Role;
import com.mju.byc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MainController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleRepository roleRepository;
    @RequestMapping("/save")
    @ResponseBody
    public void save()
    {
        User user = new User();
        user.setId(1);
        user.setName("byc");
        user.setAge(18);
        Role role = new Role();
        role.setRole("ADMIN");
        role.setId(1);
        Role role1 = new Role();
        role1.setRole("USER");
        role1.setId(2);
        role.getUsers().add(user);
        role1.getUsers().add(user);
        user.getRoles().add(role);
        user.getRoles().add(role1);
        userRepository.save(user);
        roleRepository.save(role);
        roleRepository.save(role1);

    }
    @RequestMapping("/save2")
    @ResponseBody
    public void save2()
    {
        User user = new User();
        user.setId(2);
        user.setName("yty");
        user.setAge(20);
        Role role1 = new Role();
        role1.setRole("USER");
        role1.setId(2);
        role1.getUsers().add(user);
        user.getRoles().add(role1);
        userRepository.save(user);
        roleRepository.save(role1);

    }

}
